from flask import Flask,request,jsonify,make_response
import json
from config import Config
from flask_cors import CORS
from index import db

app = Flask(__name__)
app.config.from_object(Config)
CORS(app)

@app.route('/',methods=['POST','GET'])
def news():
    if request.method == 'POST':
        #添加
        #获取用户输入
        data = json.loads(request.data)
        print(data)
        sql = "insert into `news` (`title`) values ('%s')"% (data['title'])
        db.insert(sql)
        return 'ok'

import time
@app.before_request
def before_request():
    #获取当前url
    url = request.url
    nowtime = time.time()
    sql = "insert into `time`(url,ktime) values('%s',%s)" % (url, nowtime)
    db.insert(sql)

@app.after_request
def after_request(response):
    url = request.url
    nowtime = time.time()
    sql = "select * from time where url='%s' and jtime is null " % (url)
    res = db.findone(sql)
    sql1 = "update time set jtime='%s' where id = %d" % (nowtime, res['id'])
    db.insert(sql1)
    # 结束更新时间
    return make_response(response)





if __name__ == '__main__':
    app.run()